<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="130px"
    >
      <el-form-item label="系列号" style="margin-left:-75px" prop="parentGoodsNo">
        <el-input
          v-model="queryParams.parentGoodsNo"
          placeholder="请输入系列号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" style="margin-left:-85px" prop="stockState">
        <el-select
          v-model="queryParams.stockState"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="dict in stockStates"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item  style="margin-left:-25px" label="系列号导入时间"  prop="updateTime">
        <el-date-picker
          style="width: 230px"
          v-model="queryParams.updateTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
          value-format="yyyy-MM-dd"
          @keyup.enter.native="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="操作员" style="margin-left:-80px" prop="createBy">
        <el-select
          style="width:130px"
          v-model="queryParams.createBy"
          clearable
          filterable
          placeholder="请选择操作员"
        >
          <el-option
            v-for="(item, index) in filterable"
            :key="index"
            :label="item"
            :value="item"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="类型" style="margin-left:-80px" prop="type">
        <el-select
          style="width:130px"
          v-model="queryParams.type"
          clearable
          filterable
          placeholder="请选择类型"
        >
          <el-option
            v-for="item in typeList"
            :key="item.vlaue"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['tms:goodsParent:export']"
          >仅导出系列号
        </el-button>

        <el-button
          type="danger"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportWithChild"
          v-hasPermi="['tms:goodsParent:export']"
          >导出系列号及其零件号
        </el-button>

        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExportOnlyChild"
          v-hasPermi="['tms:goodsParent:export']"
        >仅导出零件号
        </el-button>

      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :border="true"
      :data="goodsParentList"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="系列号" align="center" prop="parentGoodsNo" />
      <el-table-column
        label="系列号状态"
        align="center"
        prop="stockState"
        :formatter="stockStateFormat"
      />
      <el-table-column
        label="类型"
        width="100"
        align="center"
        prop="type"
      />
      <el-table-column label="系列号导入时间" align="center" prop="updateTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleCheck(scope.row)"
            v-hasPermi="['tms:goodsParent:list']"
            >查看系列号下零件号</el-button
          >
        </template>
      </el-table-column>

      
      <el-table-column
        label="操作员"
        width="100"
        align="center"
        prop="createBy"
      />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[10,20,30,50,200,500]"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改驾驶室管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="系列号" prop="parentGoodsNo">
          <el-input v-model="form.parentGoodsNo" placeholder="请输入系列号" />
        </el-form-item>
        <el-form-item label="状态" prop="stockState">
          <el-select v-model="form.stockState" clearable placeholder="请选择">
            <el-option
              v-for="dict in stockStates"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <CheckParent
      :checkLoading="checkLoading"
      :record="record"
      @refresh="refresh"
    ></CheckParent>
  </div>
</template>

<script>
import {
  addGoodsParent,
  delGoodsParent,
  getGoodsParent,
  listGoodsParent,
  updateGoodsParent,
} from "@/api/tms/goodsParent";
import { getPeople} from "@/api/tms/inventory";
import CheckParent from "./components/CheckParent.vue";
export default {
  name: "GoodsParent",

  data() {
    return {
      stockStates: [
        {
          label: "未出库",
          value: 0,
        },
        {
          label: "已出库",
          value: 1,
        },
      ],
      typeList:[
        {label:'驾驶室',
          value:0
        },
        {label:'轮胎',
          value:1
        }
      ],
      // 遮罩层
      loading: true,
      checkLoading: false,
      record: {},
      // 操作员
      filterable: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 驾驶室管理表格数据
      goodsParentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        parentGoodsNo: this.$route.query.parentGoodsNo,
        stockState: null,
        status: null,
        updateTime:[],
        startTime:"",
        endTime:"",
        deleted: null,
        createBy:"",
        type:"",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentGoodsNo: [
          { required: true, message: "系列号不能为空", trigger: "blur" },
        ],
        stockState: [
          { required: true, message: "状态不能为空", trigger: "blur" },
        ],
      },
    };
  },
  components: {
    CheckParent,
  },
  created() {
    this.getList();
  },
  watch:{
    "queryParams.updateTime"(newVal){
      if (newVal == null) {
        this.queryParams.updateTime=[]
      }
    }
  },
  methods: {
    getGoods(data){
      listGoodsParent(data).then((response) => {
        this.goodsParentList = response.rows;
        for (let i = 0; i < this.goodsParentList.length; i++) {
          if (this.goodsParentList[i].type == 1) {
            this.goodsParentList[i].type = '轮胎'
          }else{
            this.goodsParentList[i].type = '驾驶室'
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
     // 表单隔行变色
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "success-row";
      } else {
        return "warning-row";
      }
    },
    /** 查询驾驶室管理列表 */
    getList() {
      getPeople().then((res) => {
        this.filterable = res.data;
      });
      this.loading = true;
      if (this.queryParams.updateTime.length>0) {
        let data={
          parentGoodsNo:this.queryParams.parentGoodsNo,
          stockState:this.queryParams.stockState,
          startTime:`${this.queryParams.updateTime[0]} 00:00:00`,
          endTime:`${this.queryParams.updateTime[1]} 23:59:59`
        }
        this.getGoods(data)
      }else {
        this.getGoods(this.queryParams)
      }
    },
    stockStateFormat(row, column) {
      return row.stockState == 0 ? "未出库" : "已出库";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentGoodsNo: null,
        stockState: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        deleted: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加驾驶室管理";
    },
    // 查看按钮
    handleCheck(record) {
      this.checkLoading = true;
      this.record = record;
    },
    refresh() {
      this.checkLoading = false;
      this.getList();
    },
    /** 修改按钮操作 */
    /* handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getGoodsParent(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改驾驶室管理";
      });
    }, */
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateGoodsParent(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoodsParent(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除驾驶室管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delGoodsParent(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "tms/goodsParent/export",
        {
          ...this.queryParams,
        },
        `系列号_${new Date().getTime()}.xlsx`
      );
    },

    /** 导出系列号按钮操作 */
    handleExportWithChild() {
      this.download(
        "tms/goodsParent/exportWithChild",
        {
          ...this.queryParams,
        },
        `系列号及其零件号_${new Date().getTime()}.xlsx`
      );
    },

    /** 导出系列号按钮操作 */
    handleExportOnlyChild() {
      this.download(
        "tms/goodsParent/exportOnlyChild",
        {
          ...this.queryParams,
        },
        `零件号_${new Date().getTime()}.xlsx`
      );
    },

  },
};
</script>
<style>
.el-table .warning-row {
  background: #f7faff;
}
.el-table .success-row {
  background: #ebf1fb;
}
</style>
